from pyspark import SparkConf, SparkContext
import os

if __name__ == '__main__':
    os.environ["PYSPARK_PYTHON"] = "D:/Python/Python310/python.exe"
    conf = SparkConf().setAppName("create rdd").setMaster("local[*]")
    sc = SparkContext(conf=conf)

    file = sc.textFile("D:/hello.txt")
    word = file.flatMap(lambda x: x.split(" "))
    word_one = word.map(lambda x: (x, 1))
    rdd = word_one.reduceByKey(lambda a, b: a + b)
    print(rdd.collect())
    sort_rdd = rdd.sortBy(lambda x: x[1], ascending=False, numPartitions=1)
    print(sort_rdd.collect())
